/*
* 目的：场景一系列操纵都写在这里
*    按键事件：
*       鼠标左键按住之后，可拖拽视口
*       鼠标右键按住之后，旋转视口
*       鼠标滚轮放大、缩小视口
*
*       鼠标左键点击，用来选择建筑物
*
*
*
* API参考：http://blog.csdn.net/besily/article/details/5033846
*
* */

MyKeys = function (camera, domElement) {

    // 变量
    this.camera = camera
    this.domElement = ( domElement !== undefined ) ? domElement : document;
    this.mousepressed = -1
    this.lastx = 0
    this.lasty = 0
    this.dragfactor = 0.1
    this.zoomfactor = 1

    // 函数
    this.keydown = function (event) {
        switch ( event.keyCode ) {
            case 87: /*W*/ console.log("keydown: W"); break;
            case 65: /*A*/ console.log("keydown: A"); break;
            case 83: /*S*/ console.log("keydown: S"); break;
            case 68: /*D*/ console.log("keydown: D"); break;
        }
    }
    this.mousedown = function (event) {
        event = event || window.event

        var x = (event.pageX || event.clientX);
        x -= this.domElement.offsetLeft;
        var y = (event.pageY || event.clientY);
        y -= this.domElement.offsetTop;

        console.log("mousedown. btn=" + event.button + ", abs pos=(" + event.x + "," +event.y + ")" + ", rel pos:"
        + x + "," + y)

        this.mousepressed = event.button
    }
    this.mousemove = function(event) {
        // console.log("mousemove. pos=(" + event.x + "," +event.y + ")")

        deltax = 0
        deltay = 0
        if(this.lastx == 0 && this.lasty == 0) {

        } else {
            deltax = event.x - this.lastx
            deltay = event.y - this.lasty
        }
        this.lastx = event.x
        this.lasty = event.y

        console.log(deltax + "," + deltay)

        if (this.mousepressed == 0) {
            // camera.position.x += factor * 1;
            factor = this.dragfactor
            camera.position.x += deltax * factor
            camera.position.y += deltay * factor
        }
    }
    this.mouseup = function (event) {
        console.log("mouseup. btn=" + event.button)
        this.mousepressed = -1
    }
    this.mousewheel = function (event) {
        console.log("mouse wheel. delta=" + event.wheelDelta)

        // 缩放摄像机
        factor = 1
        if (event.wheelDelta < 0) {
            factor = -1
        }
        camera.position.z += factor * this.zoomfactor;


        // 阻止将事件传递给下一层
        event.preventDefault();
    }
    this.mouseout = function () {
        console.log("mouse out")
        this.lastx = 0
        this.lasty = 0
    }


    function bind( scope, fn ) {

        return function () {

            fn.apply( scope, arguments );

        };

    }
    function contextmenu( event ) {
        console.log("right menu event.")
        event.preventDefault();

    }


    var _keydown = bind( this, this.keydown );
    var _mousedown = bind( this, this.mousedown );
    var _mousemove = bind( this, this.mousemove );
    var _mouseup = bind( this, this.mouseup );
    var _mousewheel = bind( this, this.mousewheel );
    var _mouseout = bind( this, this.mouseout );

    // 抄过来的，不知道会不会执行！
    this.dispose = function () {
        console.log("keys dispose.")
        this.domElement.removeEventListener('mousedown', _mousedown, false );
        this.domElement.removeEventListener('mousemove', _mousemove, false );
        this.domElement.removeEventListener('mouseup', _mouseup, false );
        window.removeEventListener('keydown', _keydown, false );
        this.domElement.removeEventListener('wheel', _mousewheel, false );

        this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
        this.domElement.removeEventListener( 'mouseout', _mouseout, false );
    }

    this.domElement.addEventListener('mousedown', _mousedown, false );
    this.domElement.addEventListener('mousemove', _mousemove, false );
    this.domElement.addEventListener('mouseup', _mouseup, false );
    window.addEventListener('keydown', _keydown, false );
    this.domElement.addEventListener('wheel', _mousewheel, false );
    this.domElement.addEventListener('mouseout', _mouseout, false );

    // 右键菜单事件
    this.domElement.addEventListener( 'contextmenu', contextmenu, false );
}

